import React from 'react'
// =====================================================================
import Encrypt from '@com/plugins/encrypt'
// ===================================================================== Temp
import FormModal from '#cpt/temp/FormModal'
// ===================================================================== global declare
const { $fn, $http, $Lazy } = window
// ===================================================================== $Lazy
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
const message 	= $Lazy.file(()=>import('@antd/message'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data: [],
		formData : [
			{ label: '领取科室',		name: 'team_name',		full:true,	required:true,	type:'select',	idStr:'name' },
			{ label: '条码号',		name: 'spec_code',	half:true,	required:true, 	min:12,	max:14,	onValue:({value, formData})=>{
				this.formRef.setValue({ project_name: '' })
				if(value.trim().length === 12 || value.trim().length === 14){
					$http.submit(null,'receive/singlePullList',{ param:{ spec_code:value }}).then(data=>{
						this.query.project_name = data.team
						this.submit.uuid = data.uuid
						this.formRef.setValue({ project_name: this.query.project_name,})
					})
				}
			} },
			{ label: '条码科室',	name: 'project_name',	half:true,	disabled:true,	p:' ' },
			{ label: '姓名', 	name:'name', 			full:true,	p:' ',			disabled:true },
			{ label: '用户名',	name: 'account',		half:true,	required:true,	onBlur:(value)=>{this.getName(value)} },
			{ label: '密码',	name: 'password',		half:true,	required:true,	type: 'password',	onBlur:(value)=>{this.getName(value)} },
		],
	}	
	query = {}
	submit = {}
	
	open = ()=> {
		this.getSelect()
		this.modal.open()
		const userData = $fn.getUser()
		const { empl_name, real_name } = userData
		const m = $fn.longSave('remember')
		if($fn.hasObject(m)){
			for(var i in m){ m[i] = Encrypt.decode(m[i]) }
		}
		this.formRef.setValue({account: empl_name, password: m?m.password:'', name:real_name })
	}
	
	// 获取下拉数据
	getSelect = () => {
		const { formData } = this.state
		$cache.then( async f => {
			// 科室
			$fn.setRow( formData, 'team_name', await f.getCache({ api:'ke_shi' }) )
			
			this.setState({ formData })
		})
	}
	// 根据账号获取姓名
	getName = (value)=>{
		if(value){
			const formValue = this.formRef.getValue(['account','password'])
			$http.submit(null, 'employee/getuserrealname', {param:{phone:formValue.account,password:formValue.password},loading:false }).then((data)=> {
				if(!$fn.hasArray(data)){
					if(data.length === 0){
						this.formRef.setValue({name:''})
					}else{
						this.formRef.setValue({name:data.real_name})
					}
				}
			})
		}
	}
	
	render(){
		const {uuid } = this.query
		return FormModal.call(this, {title:'领取', labelWidth:'s5', width:700, api:'receive/singleAdd', 
					okDisabled:uuid ? true : false, okText:'领取', modal: false,
					isConfirm: ()=> {
						const formData = this.formRef.getValue(['team_name','project_name'])
						if(formData.team_name!==formData.project_name){
							message.then(f=>f.error('科室不正确'))
							return false
						}else{
							return true
						}
					},
					onClear: ()=>{
						this.formRef.reset(['spec_code','project_name'])
					}
				} )
	}
}

export default Index